<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\setting\controller;
use think\Cache;
use think\Request;
use mip\Htmlp;
use mip\AdminApiBase;
class ApiAdminMainmenu extends AdminApiBase
{
    protected $beforeActionList = ['start'];
    public function start() {

    }

    public function get_list()
    {
        $mainmenu = model('app\setting\model\Mainmenu')->getList(true);
        return jsonSuccess('主菜单信息获取成功',['mainmenu' => $mainmenu]);
    }

    public function do_save(){

        $id = input('post.id');
        $pid = input('post.pid')==''?0:input('post.pid');
        $item_title = input('post.item_title');
        $item_url = input('post.item_url');
        $item_status= input('post.item_status');
        $saveStatus= input('post.saveStatus');

        $arr= ['pid' => $pid,'item_title' => $item_title,'item_url' => $item_url,'item_status' => $item_status];

        if($saveStatus == 'add'){
            $arr['create_time']= date('Y-m-d H:i:s',time());
            $arr['edit_time']= date('Y-m-d H:i:s',time());

            model('app\setting\model\Mainmenu')->doAdd($arr);

        }else{
            $arr['edit_time']= date('Y-m-d H:i:s',time());
            $arr['id'] = $id;

            model('app\setting\model\Mainmenu')->doEdit($arr);
        }
        Cache::clear('main_menu');

        return jsonSuccess('菜单项保存成功');

    }

    public function do_sort_save()
    {
        $itemList = input('post.itemList/a');
        if ($itemList) {
            foreach ($itemList as $key => $val) {
                if ($itemListInfo = model('app\setting\model\Mainmenu')->getById($val['id']) ) {
                    model('app\setting\model\MainMenu')->doEdit(array('id'=>$val['id'],'sort' => $val['sort']));
                }
                if ($itemList[$key]['children']) {
                    foreach ($itemList[$key]['children'] as $k => $v) {
                        if (model('app\setting\model\Mainmenu')->getById($v['id']) ) {
                            model('app\setting\model\Mainmenu')->doEdit(array('id'=>$v['id'] ,'sort' => $v['sort']));
                        }
                    }
                }
            }

            return jsonSuccess('保存成功');
        }

    }

    public function do_del(){
        $id = input('post.id');
        model('app\setting\model\Mainmenu')->doDelById($id) ;
        return jsonSuccess('菜单项删除成功!');
    }

}